package com.example.flashsales.controller;


import com.example.flashsales.Tools.TokenTool;
import com.example.flashsales.entity.Result;
import com.example.flashsales.entity.dto.StoreDto;
import com.example.flashsales.entity.vo.StoreCostRecordVo;
import com.example.flashsales.entity.vo.StoreVo;
import com.example.flashsales.service.impl.StoreCostRecordServiceImpl;
import com.example.flashsales.service.impl.StoreServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin
@RequestMapping("/store")
public class StoreController {
    @Autowired
    StoreServiceImpl storeService;

    @Autowired
    StoreCostRecordServiceImpl storeCostRecordService;


    /**
     * 密码登录
     * @param storeDto
     * @param httpServletResponse
     * @param httpServletRequest
     * @return
     */
    @ResponseBody
    @PostMapping("/loginByAccountAndPassword")
    public Result loginByAccountAndPassword(@RequestBody StoreDto storeDto, HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest){
        httpServletResponse.setHeader("Access-Control-Allow-Headers", "x-requested-with,Content-Type");
        httpServletResponse.setHeader("Access-Control-Allow-Origin", httpServletRequest.getHeader("origin"));
        httpServletResponse.setHeader("Access-Control-Allow-Methods", "POST,GET");
        httpServletResponse.addHeader("Access-Control-Expose-Headers", "Token");
        if (storeDto == null ||storeDto.getPassword() == null || storeDto.getPhone() == null || storeDto.getPhone().equals("")|| storeDto.getPassword().equals("")){
            return Result.error("账号密码不能为空");
        }
        final StoreVo storeVo = storeService.loginByAccountAndPassword(storeDto.getPhone(), storeDto.getPassword());
        if (storeVo == null){
            return Result.error("账号或密码错误");
        }
        Map<String,String> map = new HashMap<>();
        map.put("id",storeVo.getId()+"");
        map.put("phone",storeVo.getPhone());
        final String token = TokenTool.createToken(map);
        httpServletResponse.setHeader("Token",token);
        return Result.ok("登录成功").setData(storeVo);
    }


    /**
     * 店家查看自己的信息
     * @param httpServletRequest
     * @return
     */
    @ResponseBody
    @GetMapping("/viewStoreMessage")
    public Result viewStoreMessage(HttpServletRequest httpServletRequest){
            long id = Integer.parseInt(httpServletRequest.getAttribute("id") + "");
            final StoreVo storeVo = storeService.viewStoreMessage(id);
            if (storeVo == null){
                return Result.error("没有此店家");
            }
            return Result.ok("查看成功").setData(storeVo);


    }


    /**
     * 原密码修改
     * @param newPassword
     * @param oldPassword
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/modifyPasswordByOldPassword")
    @ResponseBody
    public Result modifyPasswordByOldPassword(String newPassword,String oldPassword,HttpServletRequest httpServletRequest){
            long id = Integer.parseInt(httpServletRequest.getAttribute("id") + "");
            final String password = storeService.modifyPasswordByOldPassword(newPassword, oldPassword, id);
            if (password.equals("修改成功")){
                return Result.ok(password);
            }
            return Result.error(password);

    }


    /**
     * 查看自己的余额
     * @param httpServletRequest
     * @return
     */
    @ResponseBody
    @GetMapping("/viewMoney")
    public Result viewMoney(HttpServletRequest httpServletRequest){
        long id = Integer.parseInt(httpServletRequest.getAttribute("id") + "");
        final Map<String, Double> map = storeService.viewMoney(id);
        if (map == null){
            return Result.error("查看失败");
        }
        return Result.ok("查看成功").setData(map);
    }


    /**
     * 查看店家的邀请码
     * @param httpServletRequest
     * @return
     */
    @ResponseBody
    @GetMapping("/viewInviteCode")
    public Result viewInviteCode(HttpServletRequest httpServletRequest){
        long id = Integer.parseInt(httpServletRequest.getAttribute("id") + "");
        final String s = storeService.viewInviteCode(id);
        if (s == null){
            return Result.error("查看失败，店家不存在");
        }
        return Result.ok("查看成功").setData(s);
    }





}
